/**	
 * Title: CSE 5322 - Software Design Patterns
 * Description: 
 * Copyright: Copyright (c) UT Arlington, 2011
 * @author: Duy Nguyen(duyanh@uta.edu), Vivek Maheshwari(vivekm@uta.edu), Arjun Satish(arjun.satish@mavs.uta.edu), Lakshmana Srinivasa(lakshmana.srinivasa@mavs.uta.edu), Ismat Jahan(ismat.jahan@mavs.uta.edu)
 * @version 1.0
 */
package edu.uta.cse5322_2011_team3;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NLPPhraseParser implements PhraseParserImpl {

	public ArrayList<Phrase> identify(Document document) {
		ArrayList<Phrase> phrases = new ArrayList<Phrase>();

		ArrayList<String> sentences = document.toSentences();

		NLPParser parser = NLPParser.getInstance();
		
		for (String sentence : sentences) {
			String taggedSentence = parser.parse(NLPParser.normalizeRequirement(sentence));
			ArrayList<String> nouns = searchNouns(taggedSentence);
			
			for(String noun: nouns) {
				System.out.println("NOUN: " + noun);
				Phrase phrase = new Noun();
				phrase.setContent(noun);
				phrases.add(phrase);
			}
			
			ArrayList<String> verbs = searchVerbs(taggedSentence.toString().trim());
			for(String verb: verbs) {
				System.out.println("VERB: " + verb);
				Phrase phrase = new Verb();
				phrase.setContent(verb);
				phrases.add(phrase);
			}

		}

		return phrases;
	}

	private ArrayList<String> searchNouns(String sentence) {
		ArrayList<String> nouns = new ArrayList<String>();
		Pattern p = Pattern.compile("(\\w+/JJ\\s)?(\\w+/NN\\s?)+");
        Matcher m = p.matcher(sentence);
        while (m.find()) {
        	nouns.add(NLPParser.removeTags(m.group()));
        }
        return nouns;
	}
	
	private ArrayList<String> searchVerbs(String sentence) {
		ArrayList<String> verbs = new ArrayList<String>();
		Pattern p = Pattern.compile("\\w+/VB[A-Z]?");
        Matcher m = p.matcher(sentence);
        while (m.find()) {
        	String verb = NLPParser.removeTags(m.group());
        	if( !verb.equals("is") && !verb.equals("are") && !verb.equals("am") && !verb.equals("was") && !verb.equals("were") && !verb.equals("be") ){
        		verbs.add(verb);
        	}
        	
        }
        return verbs;
	}
	
	
}
